package com.fearless.crm.workbench.web.controller;

import com.fearless.crm.settings.entity.User;
import com.fearless.crm.settings.service.UserService;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.workbench.entity.*;
import com.fearless.crm.workbench.service.*;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("tran")
@Controller
public class TranController {

    @Resource
    private UserService userService;

    @Resource
    private ActivityService activityService;

    @Autowired
    private TranService tranService;

    @Autowired
    private TranHistoryService tranHistoryService;

    @Autowired
    private ContactsService contactService;

    @Autowired
    private CustomerService customerService;

//铺垫新建交易的模态窗口
    @RequestMapping("/add.do")
    public ModelAndView addController(){
        System.out.println("进入到addController处理器方法中");
        System.out.println("铺垫新建交易的模态窗口");

        List<User> userList=userService.getUserListService();

        ModelAndView mv=new ModelAndView();
        mv.addObject("userList",userList);
        mv.setViewName("forward:/workbench/transaction/save.jsp");
        return mv;
    }


//根据名称模糊查询市场活动
    @RequestMapping("/searchActivity.do")
    @ResponseBody
    public List<Activity> searchActivityController(String name){
        System.out.println("进入到searchActivityController处理器方法");
        System.out.println("根据市场活动名称模糊查询市场活动");
        List<Activity> activityList=activityService.queryActivityByNameService(name);


        return activityList;
    }


//根据名称模糊查询联系人
    @RequestMapping(value = "/searchContact.do",method = RequestMethod.GET)
    @ResponseBody
    public List<Contacts> searchContactController(String cname){
        System.out.println("进入到searchContactController处理器方法");
        System.out.println("根据名称模糊查询联系人");

        List<Contacts> contactsList=contactService.searchContactsByNameService(cname);

        return  contactsList;
    }


//自动补全
    @RequestMapping(value = "/getCustomerName.do",method = RequestMethod.GET)
    @ResponseBody
    public List<String> getCustomerNameAutoController(String name){
        System.out.println("进入到getCustomerNameAutoController处理器方法");
        System.out.println("自动补全操作");

        List<String> customerList=customerService.getCustomerNameAuto(name);

        return customerList;
    }


//创建交易
    @RequestMapping(value = "/save.do",method = RequestMethod.POST)
    public ModelAndView saveTranController(HttpSession session,Tran tran,String customerName){
        System.out.println("进入到saveTranController处理器方法");
        System.out.println("执行交易的添加操作");
        String model=tran.toString();
        System.out.println("model"+model);
        String createTime= DateTimeUtil.getSysTime();
        String id= UUIDUtil.getUUID();
        String createBy=((User)session.getAttribute("user")).getName();

        tran.setId(id);
        tran.setCreateBy(createBy);
        tran.setCreateTime(createTime);

        boolean flag=tranService.saveTranService(tran,customerName);

        ModelAndView mv=new ModelAndView();
        if(flag){
            mv.setViewName("redirect:/workbench/transaction/index.jsp");
        }

        return mv;
    }


//修改交易,铺垫修改交易的模态窗口
    @RequestMapping(value = "/editTran.do",method = RequestMethod.GET)
    public ModelAndView editTranController(String tranId){
        System.out.println("进入到editTranController处理器方法");
        System.out.println("用于铺垫修改交易的模态窗口");

        Tran tran=tranService.getTranByIdService(tranId);
       List<User> userList=userService.getUserListService();

        ModelAndView mv=new ModelAndView();
        Map<String,Object> map=new HashMap<>();
        map.put("tranInRequestScope",tran);
        map.put("userList",userList);

        mv.addObject("requestScopeMap",map);
        mv.setViewName("forward:/workbench/transaction/edit.jsp");
        return mv;
    }


//刷新交易列表
    @RequestMapping(value = "/showTranList.do",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> showTranListController(Tran tran){
        System.out.println("进入到showTranListController处理器方法");
        System.out.println("刷新交易列表");
        Map<String,Object> map=tranService.showTranListService(tran);

       System.out.println("这里的参数"+tran.toString());


        return map;
    }

//铺垫条件查询的所有者下拉框
    @RequestMapping(value = "/fillOwner.do",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> fillOwnerController(){
        System.out.println("进入到fillOwnerController处理器方法");
        System.out.println("执行铺垫条件查询所有者下拉框的操作");

        List<User> userList=userService.getUserListService();
        Map map=new HashMap();
        map.put("userList",userList);

        return map;
    }


//跳转到详细页
    @RequestMapping("detail.do")
    public ModelAndView detailTranController(HttpServletRequest request,String id){
        System.out.println("进入到detailTranController处理器方法");

        System.out.println("接受到的参数"+id);

       Tran tran= tranService.getTranByIdService(id);
       /*
            处理可能性

            首先我们要获取到阶段,因为阶段和可能性是键值对的关系
                阶段和可能性之间的对应关系:pMap   位于服务器缓存之中(上下文作用域/全局作用域)

        */
        String stage=tran.getStage();
        ServletContext application=request.getServletContext();//获取全局作用域
        Map<String,String> map=(Map<String, String>)application.getAttribute("pMap");
        String possibility=map.get(stage);
        System.out.println("可能性是"+possibility);

        ModelAndView mv=new ModelAndView();
        mv.addObject("tran",tran);
        mv.addObject("possibility",possibility);
        mv.setViewName("forward:/workbench/transaction/detail.jsp");



        return mv;
    }



//展现交易历史列表,局部刷新
    @RequestMapping(value = "/getHistoryListByTranId.do",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getHistoryListByTranIdController(HttpServletRequest request,String tranId){
        System.out.println("进入到getHistoryListByTranIdController处理器方法");
        System.out.println("展现交易历史列表");

        List<TranHistory> tranHistoryList=tranHistoryService.getHistoryListByTranIdService(tranId);


        //在服务器缓存(上下文作用域)拿到管理阶段和可能性的pMap
        ServletContext application=request.getServletContext();
        Map<String,String> pmap=(Map<String, String>)application.getAttribute("pMap");

        //通过阶段得到可能性。首先,我们获取阶段
        for(TranHistory tranHistory:tranHistoryList){
            String stage=tranHistory.getStage();//获取阶段

            String possibility= pmap.get(stage);//获取阶段对应的可能性

            tranHistory.setPossibility(possibility);//将可能性封装到交易历史对象中

        }



        Map<String,Object> map=new HashMap<>();
        map.put("tranHistoryList",tranHistoryList);
        return map;
    }


//改变阶段
    @RequestMapping("/changeStage.do")
    @ResponseBody
    public Map<String,Object>changeStageController(HttpServletRequest request,HttpSession session,Tran tran){
        System.out.println("进入到changeStageController处理器方法");
        System.out.println("执行修改交易阶段的操作");

        //System.out.println("接受到的参数"+tran.toString());
        String editBy=((User)session.getAttribute("user")).getName();
        String editTime=DateTimeUtil.getSysTime();

        tran.setEditBy(editBy);
        tran.setEditTime(editTime);

        boolean flag=tranService.changeTranStage(tran);

        //获取阶段和可能性的关系键值对pMap
        ServletContext application=request.getServletContext();
        Map<String,String> pMap=(Map<String, String>) application.getAttribute("pMap");
        String possibility=pMap.get(tran.getStage());//获取可能性

        tran.setPossibility(possibility);
        Map<String,Object> map=new HashMap<>();
        map.put("success",flag);
        map.put("tranObject",tran);

        return map;
    }


//漏斗图的展现
    @RequestMapping("/getCharts.do")
    @ResponseBody
    public Map<String,Object>  getChartsController(){
        System.out.println("进入到getChartsController处理器方法");
        System.out.println("取得交易阶段数量统计图表的数据");


        Map<String,Object> map=tranService.getChartsService();

        return map;
    }



   public String testValue(HttpServletResponse response){

       Cookie cookie = new Cookie("5", "45");
       cookie.setMaxAge(60*60);

       response.addCookie(cookie);
       return null;
   }

}
